﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Rison.DataAccess.Data.ZJCP;
using System.Text;

namespace Rison.Web.ZJCP.ashx
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class SaveVote : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string comment = context.Request.Params["comment"];
            string department = context.Request.Params["department"];
            string coContactWay = context.Request.Params["coContactWay"];
            string coId = System.Guid.NewGuid().ToString();
            int intCount = saveComment(coId, comment, department, coContactWay);
            if (intCount <= 0)
            {
                coId = "";
            }
            string departId = "";
            DataSet ds = findAllDeparts();
            StringBuilder sql = new StringBuilder();
            
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                departId = ds.Tables[0].Rows[i][0].ToString();
                string voGrade = context.Request.Params[departId];
                if (!String.IsNullOrEmpty(voGrade))
                {
                    sql.Append(String.Format(" insert into Vote values(newid(),'{0}','{1}','{2}') ", departId, voGrade, coId));
                }

            }
            if (sql.Length > 10)
            {
                saveVote(sql.ToString());
            }
            context.Response.Redirect(String.Format("../Vote.aspx?_={0}", DateTime.Now.ToString("yyyyMMddHHmmssffff")));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private DataSet findAllDeparts()
        {
            string sql = "select DepartId from Department where ifShow=1";
            return DbAccess.executeQuery(sql);
        }
        private int saveComment(string coId, string comment, string departId, string coContactWay)
        {
            if (String.IsNullOrEmpty(comment) && String.IsNullOrEmpty(departId) && String.IsNullOrEmpty(coContactWay))
            {
                return 0;
            }
            string sql = String.Format("insert into Comment values('{0}',{1}, '{2}', {3})", coId, DbAccess.parse(comment), departId, DbAccess.parse(coContactWay));
            return DbAccess.executeUpdate(sql);
        }
        private int saveVote(string sql)
        {
            return DbAccess.executeUpdate(sql);
        }
    }
}
